#Tyler Moore and JT Hamrick
#Script for analyzing data on rise and fall of cryptocurrency coins.
#Gandal, N., Hamrick, J.T., Moore, T. et al. The rise and fall of cryptocurrency coins and tokens. Decisions Econ Finan (2021). https://doi.org/10.1007/s10203-021-00329-8



options(scipen=999)
library(data.table)
library(zoo)
library(dplyr)
library(survival)


ac<-fread("abandonrangesuniqWEIS18v2.csv")
abmo<-fread("abmo.csv")
cv<-fread("coin_data.csv")
ppp<-fread("price_prepost.csv")
vpp<-fread("volume_prepost.csv")
pv<-fread("volume_5P.csv")
pp<-fread("price_5P.csv")
coins.avgvol <- fread("coin_avgvol.csv")
moav<-fread("moavol.csv")

#this peak calculation is flawed because it only includes peaks for currencies that are abandoned. though that could be interesting for comparison sake
ac$peakday<-as.Date(ac$peakday,"%Y-%m-%d")
ac$peakmon<-cut(ac$peakday,breaks="month")
ac$peakday<-as.Date(ac$peakday,"%Y-%m-%d")
ac$peakmon<-cut(ac$peakday,breaks="month")
ac$resmonth<-as.Date(ac$resmon)

acr<-ac[ac$resurrected,]
numres<-aggregate(resvol~resmonth,length,data=acr)
names(numres)<-c("month","numres")

acpv<-unique(ac[,c("coin","peakvol")])

acpv[acpv$peakvol>10000000,]

pp$peakd<-as.Date(pp$peak,"%Y-%m-%d")
pp$peakmon<-cut(pp$peakd,breaks="month")

pv$peakd<-as.Date(pv$peak,"%m/%d/%y")
pv$peakmon<-cut(pv$peakd,breaks="month")

moav$mon<-factor(as.Date(moav$mo,"%m/%d/%y"))

numcoins<-aggregate(moavol~mon,length,data=moav)
names(numcoins)<-c("month","numcoins")
numcoins$month<-as.Date(numcoins$month,"%Y-%m-%d")

abmo$abmonth<-as.Date(abmo$abmonth,"%Y-%m-%d")
numab<-aggregate(coin~abmonth,length,data=abmo)
names(numab)<-c("month","numabcoins")

alivedead<-merge(numcoins,numab,by="month",all.x=T)
alivedead$numabcoins[is.na(alivedead$numabcoins)]<-0
alivedead$numalive<-alivedead$numcoins-alivedead$numabcoins

nc2<-aggregate(moavol~mon+market,length,data=moav)
nc2$mon<-as.Date(nc2$mon)
abmo$null<-1
na2<-aggregate(null~abmonth+coin,length,data=abmo)
nac3<-merge(nc2,na2,by.x=c("market","mon"),by.y=c("coin","abmonth"))
abactive<-aggregate(moavol~mon,length,data=nac3)
names(abactive)<-c("month","numactiveab")

alivedead<-merge(alivedead,abactive,by="month")
alivedead$numcoinsad<-alivedead$numcoins+alivedead$numabcoins-alivedead$numactiveab
alivedead$frab<-alivedead$numabcoins/alivedead$numcoinsad

numpeakp<-aggregate(peak_value~peakmon,length,data=pp)
names(numpeakp)<-c("month","numpeakprice")
numpeakp$month<-as.Date(numpeakp$month,"%Y-%m-%d")

numpeakv<-aggregate(peak_value~peakmon,length,data=pv)
names(numpeakv)<-c("month","numpeakvol")
numpeakv$month<-as.Date(numpeakv$month,"%Y-%m-%d")

numpeaks<-merge(numcoins,numpeakp,by="month")
numpeaks<-merge(numpeaks,numpeakv,by="month")
ad2<-alivedead[,c("month","numalive")]
numpeaks<-merge(numpeaks,ad2,by="month",all.x=T)
numpeaks$frpeakprice<-numpeaks$numpeakprice/numpeaks$numalive
numpeaks$frpeakvol<-numpeaks$numpeakvol/numpeaks$numalive

# get all market data for aggregate values
cv$market_cap <- as.numeric(gsub(",", "", cv$market_cap))
cv$volume <- as.numeric(gsub(",", "", cv$volume))
cv$date <- as.Date(cv$date, "%d-%b-%y")

# get first record for each coin
cvfd <- cv[cv[, .I[which.min(date)], by=market]$V1]
cvfd <- subset(cvfd, select=-c(tag, unix_time, high, low, close, market_cap))
colnames(cvfd) <- c("market", "fdate", "fopen", "fvolume")

pv.first <- pv[pv[, .I[which.min(peakd)], by=market]$V1]
pp.first <- pp[pp[, .I[which.min(peakd)], by=market]$V1]

pv.first <- subset(pv.first, select=-c(run_type, peak))
pp.first <- subset(pp.first, select=-c(run_type, peak))
colnames(pv.first) <- c("market", "v_peak_value", "v_peakd", "v_peakmon")
colnames(pp.first) <- c("market", "p_peak_value", "p_peakd", "p_peakmon")

coinsummary <- cvfd
coinsummary <- merge(coinsummary, pp.first, by.x="market", by.y="market", all=T)
coinsummary <- merge(coinsummary, pv.first, by.x="market", by.y="market", all=T)

# calculate time to first peak
coinsummary$p_dtfp <- as.numeric(coinsummary$p_peakd - coinsummary$fdate)
coinsummary$v_dtfp <- as.numeric(coinsummary$v_peakd - coinsummary$fdate)

coinsummary$fopen <- as.numeric(coinsummary$fopen)

# get percent increase (1st day to peak)
coinsummary$p_peak_value <- as.numeric(coinsummary$p_peak_value)
coinsummary$p_pcti <- (coinsummary$p_peak_value - coinsummary$fopen) / coinsummary$fopen
coinsummary$v_pcti <- (coinsummary$v_peak_value - coinsummary$fvolume) / coinsummary$fvolume

# get time to 1st abandonment
ac$abmon <- as.Date(ac$abmon)
ac.firstab <- ac[ac[, .I[which.min(abmon)], by=coin]$V1]
coinsummary$res <- FALSE
coinsummary$resmon <- character()
coinsummary$fab <- character()
for (item in 1:nrow(coinsummary)) {
    index = which(ac.firstab$coin == coinsummary[item]$market)
    if (length(index) > 0) {
        # convert date to string or R will give you some useless number
        coinsummary[item]$fab <- as.character(ac.firstab$abmon[index])
        coinsummary[item]$res <- as.character(ac.firstab$resurrected[index])
        coinsummary[item]$resmon <- as.character(ac.firstab$resmon[index])
    }
}

coinsummary$fmo <- cut(coinsummary$fdate, breaks="month")
coinsummary$fab <- as.Date(coinsummary$fab)
coinsummary$resmon <- as.Date(coinsummary$resmon)
coinsummary$fmo <- as.Date(coinsummary$fmo)
coinsummary$v_peakmon <- as.Date(coinsummary$v_peakmon)
# open to first abandonment
coinsummary$otfa <- as.numeric(coinsummary$fab - coinsummary$fmo)
# peak to first abandonment

coinsummary$ptfa <- 12 * as.numeric(as.yearmon(coinsummary$fab) - as.yearmon(coinsummary$v_peakmon))
coinsummary$resmon <- as.Date(coinsummary$resmon)
coinsummary$ttrpa <- 12 * as.numeric(as.yearmon(coinsummary$resmon) - as.yearmon(coinsummary$v_peakmon))
# reset coins not resurrected
coinsummary$ttrpa[which(coinsummary$res==FALSE)] <- NA

pv <- subset(pv, select=-c(peak, run_type))
colnames(pv) <- c("market", "v_peak_value", "v_peakd", "v_peakmon")

# get dataset without first peak rows
pv.second <- anti_join(pv, pv.first, by=c("market", "v_peakd"))
pv.second <- data.table(pv.second)
pv.second <- pv.second[pv.second[, .I[which.min(v_peakd)], by=market]$V1]

colnames(pv.second) <- c("market", "pa_peak_value", "pa_peakd", "pa_peakmon")

#Tyler analyze coinsummary.csv
cs <- coinsummary
cs$openday<-as.Date(cs$fdate,"%Y-%m-%d")
cs$openmo<-cut(cs$openday,breaks="month")

barplot(quantile(cs$p_pcti,seq(0,1,0.1),na.rm=T))

numopen<-aggregate(fopen~openmo,length,data=cs)
names(numopen)<-c("month","newcoins")
numopen$month<-as.Date(numopen$month,"%Y-%m-%d")

numpeaks<-merge(numopen,numpeaks,by="month")

numab<-aggregate(abvol~abmon,length,data=ac)
names(numab)<-c("month","numab")
numab$month<-as.Date(numab$month,"%Y-%m-%d")

numpeaks<-merge(numab,numpeaks,by="month",all.y=T)

bitcoin <- cv[cv$market=="Bitcoin (BTC)",]
bitcoin$date <- as.Date(bitcoin$date, "%d-%b-%y")

coins.avgvol$date <- as.Date(coins.avgvol$date, "%Y-%m-%d")
                                        #edit graph below

numrespeak <- merge(numpeaks,numres,by="month",all=T)

### ADDED BY TYLER 2020-10-29
coins.avgvol2 <- coins.avgvol[, 4:5]
numrespeak <- merge(numrespeak, coins.avgvol2, by.x="month", by.y="date")
bitcoin$close<-as.numeric(bitcoin$close)
bitcoin$month<-cut(bitcoin$date,breaks="month")
bmo<-aggregate(close~month,data=bitcoin,mean,na.rm=T)
bmo$month<-as.Date(bmo$month)
numrespeak2<-merge(numrespeak,bmo,by="month")
corsub <- numrespeak2[, c(2,10,3,11,1,5,6)]
colnames(corsub) <- c("coins_abandoned", "coins_resurrected", "coins_created", "trade_volume", "btc_price", "price_peaks", "volume_peaks")
library(Hmisc)
coincor <- rcorr(as.matrix(corsub))
write.csv(numrespeak2,"coinsraf.csv",row.names=F)

#### END ADDED BY TYLER 2020-10-29


pdf('coinabf.pdf',height=9,width=8)
par(mfrow=c(5,1), oma = c(2.1,2,0,2), mar = c(2,3,1.5,3))
plot(x=numpeaks$month,y=numpeaks$newcoins, type="l", main="Coins Newly Created/Abandoned", xlab="Date", ylab="# Coins", col="black", ylim=c(0, max(numpeaks$numab,na.rm=T)), cex.lab=1.25,xlim=c(min(bitcoin$date),max(bitcoin$date)))
#mtext(side = 2, line = 3, '# Coins', cex.lab=1.25)
lines(x=numpeaks$month,y=numpeaks$numab, col="red",lty=2)
legend("topleft", legend=c("newly created", "newly abandoned"), col=c("black", "red"), lty=1:2, bg="white", cex=1.25)

plot(x=numres$month,y=numres$numres, col="blue", type="l", xlab="Date", ylab="# Coins", main="Coins Resurrected/Avg. Daily Trans Vol", cex.lab=1.25,xlim=c(min(bitcoin$date),max(bitcoin$date)),lty=1)
mtext(side = 2, line = 3, '# Coins', cex.lab=1.25)
par(new = T)
plot(coins.avgvol$date, coins.avgvol$avgvol, pch=16, axes=F, xlab=NA, ylab=NA, col="darkgreen", type="l",xlim=c(min(bitcoin$date),max(bitcoin$date)),lty=2)
axis(side = 4)
mtext(side = 4, line = 3, 'Daily Avg. Trading Volume Per Month', cex.lab=1.25)
legend("topleft", legend=c("resurrected", "avg daily transaction"), col=c("blue", "darkgreen"), lty=1:2, bg="white", cex=1.25)

plot(x=alivedead$month,y=alivedead$numcoinsad,type='l',xlim=c(min(bitcoin$date),max(bitcoin$date)),ylim=c(0,max(alivedead$numcoinsad)), cex.lab=1.2,main="Active vs. Abandoned Coins")
lines(x=alivedead$month,y=alivedead$numabcoins,type='l',lty=2)
legend("topleft", legend=c("currently active", "currently abandoned"), col=c("black", "black"), lty=1:2, bg="white", cex=1.25)

plot(x=alivedead$month,y=alivedead$frab,type='l',xlim=c(min(bitcoin$date),max(bitcoin$date)),main="Fraction Coins Abandoned", cex=1.25)

plot(bitcoin$date, bitcoin$open, type="l",log='y',xlim=c(min(bitcoin$date),max(bitcoin$date)),main="BTC price", cex=1.25)
dev.off()


pdf('peakstime.pdf',height=7,width=10)
par(mfrow=c(2,1), oma = c(2.1,2,0,2), mar = c(2,1,1.5,1))

plot(x=numpeaks$month,y=numpeaks$numpeakprice, type="l", main="Coin Peaks (#)", xlab="Date", ylab="# Coins", col="brown", ylim=c(0, max(numpeaks$numpeakprice,na.rm=T)), cex.lab=1.25)
#mtext(side = 2, line = 3, '# Coins', cex.lab=1.25)
lines(x=numpeaks$month,y=numpeaks$numpeakvol, col="orange",lty=2)
legend("topleft", legend=c("#coins price peak", "#coins volume peak"), col=c("brown", "orange"), lty=1:2, bg="white", cex=1.25)


plot(x=numpeaks$month,y=100*numpeaks$frpeakprice, type="l", main="Coin Peaks (%)", xlab="Date", ylab="# Coins", col="brown", cex.lab=1.25)
#mtext(side = 2, line = 3, '# Coins', cex.lab=1.25)
lines(x=numpeaks$month,y=100*numpeaks$frpeakvol, col="orange",lty=2)
legend("topleft", legend=c("% coins price peak", "% coins volume peak"), col=c("brown", "orange"), lty=1:2, bg="white", cex=1.25)
dev.off()


pdf('coinsummarynew.pdf')
par(mfrow=c(4,1), oma = c(2.1,2,0,2), mar = c(2,3,1.5,3))
plot(x=numpeaks$month,y=numpeaks$newcoins, type="l", main="Coins Created/Abandoned", xlab="Date", ylab="# Coins", col="black", ylim=c(0, max(numpeaks$numab,na.rm=T)), cex.lab=1.25)
mtext(side = 2, line = 3, '# Coins', cex.lab=1.25)
lines(x=numpeaks$month,y=numpeaks$numab, col="red",lty=2)
legend("topleft", legend=c("created", "abandoned"), col=c("black", "red"), lty=1:2, bg="white", cex=1.25)
dev.off()

#identify big, medium, small coins
cv[,obs:=length(date), by=market]
cv[,avgvol:=mean(volume), by=market]
coinmap <- unique(cv[obs>=90&avgvol>1000000,]$market)
coinmap$id <- seq.int(nrow(coinmap))

cv2<-aggregate(volume~market,mean,data=cv,na.rm=T)

cv2$size<-ifelse(cv2$volume<100000,"small","medium")
cv2$size[cv2$volume>=1000000]<-"big"
cv2$size<-factor(cv2$size)
names(cv2)<-c("coin","avg.daily.vol","size")

cvs<-aggregate(volume~market,sum,data=cv,na.rm=T)

cvs$size<-ifelse(cvs$volume<1000000,"m1","m1m10")
cvs$size[cvs$volume>=10000000]<-"m10m100"
cvs$size[cvs$volume>=100000000]<-"m100b1"
cvs$size[cvs$volume>=1000000000]<-"b1"

cvs$size<-ordered(cvs$size,c("m1","m1m10","m10m100","m100b1","b1"))
names(cvs)<-c("coin","totalvol","size")

length(cvs$market[cvs$volume<=1000000])

cs2<-merge(cvs,coinsummary,by.x="coin",by.y="market")

aggregate(p_pcti~size,cs2,median,na.rm=T)
aggregate(v_pcti~size,cs2,median,na.rm=T)
aggregate(otfa~size,cs2,median,na.rm=T)
aggregate(ptfa~size,cs2,median,na.rm=T)
aggregate(p_dtfp~size,cs2,median,na.rm=T)
aggregate(v_dtfp~size,cs2,median,na.rm=T)

acs<-merge(ac,cvs,by="coin",all.y=T)
table(acs$resurrected,acs$size)
prop.table(table(acs$resurrected,acs$size),2)
acs$abandoned[is.na(acs$abandoned)]<-F

table(acs$size)
table(acs$abandoned,acs$size)
100*round(prop.table(table(acs$abandoned,acs$size),2)[2,],2)
table(acs$resurrected,acs$size)[2,]
100*round(prop.table(table(acs$resurrected,acs$size),2)[2,],2)
prop.table(table(acs$abandoned,acs$size),2)

cs2$fyear<-cut(cs2$fdate,breaks="year")
plot(table(cs2$fyear,cs2$size),color=T)

cs2$size2<-ifelse(cs2$size %in% c("m1","m1m10","m10m100"),"l100m","g100m")

lastday<-aggregate(date~market,cv,max)
names(lastday)<-c("coin","lastday")
cs2no1wk<-merge(lastday,cs2,by="coin")
cs2no1wk$ttpp<-as.Date(cs2no1wk$p_peakd)-as.Date(cs2no1wk$fdate)
cs2no1wk$ttvp<-as.Date(cs2no1wk$v_peakd)-as.Date(cs2no1wk$fdate)

cs2no1wkp<-cs2no1wk[cs2no1wk$ttpp>=7,]
cs2no1wkv<-cs2no1wk[cs2no1wk$ttvp>=7,]

yo<-aggregate(p_pcti~fyear,cs2no1wkp,median,na.rm=T)
yo$size2<-"all"
ys<-aggregate(p_pcti~size2+fyear,cs2no1wkp,median,na.rm=T)
ys<-ys[3:10,]
ys2<-rbind(ys,yo[2:5,])
ys2$p_pcti<-100*ys2$p_pcti
ys2l<-ys2[ys2$size2=="l100m",]
ys2g<-ys2[ys2$size2=="g100m",]
ys2a<-ys2[ys2$size2=="all",]

vo<-aggregate(v_pcti~fyear,cs2no1wkv,median,na.rm=T)
vo$size2<-"all"
vs<-aggregate(v_pcti~size2+fyear,cs2no1wkv,median,na.rm=T)
vs<-vs[3:10,]
vs2<-rbind(vs,vo[2:5,])
vs2$v_pcti<-100*vs2$v_pcti
vs2l<-vs2[vs2$size2=="l100m",]
vs2g<-vs2[vs2$size2=="g100m",]
vs2a<-vs2[vs2$size2=="all",]

pdf("yr2peak.pdf",height=4,width=10)
par(mfrow=c(1,2), mar = c(4,4,1.5,0.5))
plot(x=as.Date(ys2g$fyear),y = ys2g$p_pcti,type='l',log='y',ylim=c(min(ys2$p_pcti),max(vs2$v_pcti)),lty=4,col=4,lwd=2,xlab="year coin introduced",ylab="median % price increase to 1st peak",main="% Price Increase Start to 1st Peak",cex.lab=1.2,xlim=c(as.Date("2014-01-01"),as.Date("2017-01-01")))
lines(x=as.Date(ys2l$fyear),y = ys2l$p_pcti,type='l',lty=2,col=2,lwd=2)
lines(x=as.Date(ys2a$fyear),y = ys2a$p_pcti,type='l',lty=1,col=1,lwd=2)
legend("topright",c("overall","total vol. < $100M","total vol. > $100M"),col=c(1,2,4),lty=c(1,2,4),lwd=2,cex=1.2)

plot(x=as.Date(vs2g$fyear),y = vs2g$v_pcti,type='l',log='y',ylim=c(min(ys2$p_pcti),max(vs2$v_pcti)),lty=4,col=4,lwd=2,xlab="year coin introduced",ylab="median % volume increase to 1st peak",main="% Volume Increase Start to 1st Peak",cex.lab=1.2,xlim=c(as.Date("2014-01-01"),as.Date("2017-01-01")))
lines(x=as.Date(vs2l$fyear),y = vs2l$v_pcti,type='l',lty=2,col=2,lwd=2)
lines(x=as.Date(vs2a$fyear),y = vs2a$v_pcti,type='l',lty=1,col=1,lwd=2)
dev.off()

#survival plot for the time to abandoment
aban<-acs[,c("coin","abandoned")]
cs3<-merge(aban,cs2,by="coin")
cs3$otfa[is.na(cs3$otfa)]<-as.numeric(as.Date("2018-02-06")-cs3$fdate)

sab<-Surv(time=cs3$otfa,event=cs3$abandoned,type="right")

ac$months2res<-as.numeric(round((ac$resmonth-ac$abmon)/30))

cssize<-cs2[,c("coin","size")]
acsize<-merge(ac,cssize,by="coin")

sres<-Surv(time=acsize$months2res,event=acsize$abandoned,type="right")

pdf("survabres.pdf",height=6,width=12)
par(mfrow = c(1,2),mar = c(4,4,1.5,0.5))
plot(survfit(sab~size,data=cs3),col=2:6,lty=2:6,lwd=2,ylab="probability coin not yet abandoned",xlab="days since coin launched",main="Time to Abandonment",cex.lab=1.3,xlim=c(0,1800))
lines(survfit(sab~1),lwd=2,conf.int=F)

plot(survfit(sres~size,data=acsize),col=2:5,lty=2:5,lwd=2,ylab="probability coin not yet resurrected",xlab="months since coin abandoned",main="Time to Resurrection",cex.lab=1.2,xlim=c(0,60))
lines(survfit(sres~1),lwd=2,conf.int=F)
#legend("topright",c("overall","<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol."),col=1:5,lty=1:5,lwd=2,bg="white")
legend("topright",c("overall","<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol."),col=1:6,lty=1:6,lwd=2,bg="white",cex=1.3)
dev.off()

coinct<-data.frame(table(acsize$coin))
names(coinct)<-c("coin","numabandon")
coinct$numabf<-factor(coinct$numabandon)
coinct2<-merge(coinct,cssize,by="coin")
barplot(table(coinct2$size,coinct2$numabf),beside=T)

acrsize<-acsize[acsize$resurrected,]
coinrct<-data.frame(table(acrsize$coin))
names(coinrct)<-c("coin","numresurrect")

coinrct$numresf<-factor(coinrct$numresurrect)
coinrct2<-merge(coinrct,cssize,by="coin")
barplot(table(coinrct2$size,coinrct2$numresf),beside=T)

pdf("numabres.pdf",height=4,width=10)
par(mfrow = c(1,2),mar = c(4,4,1.5,0.5))
barplot(table(coinct2$size,coinct2$numabf),beside=T,col=1:5,density=20,angle=seq(45,225,45),border=T,xlab="# abandonments",ylab="# coins",main="Abandonments per coin",ylim=c(0,160))
barplot(table(coinrct2$size,coinrct2$numresf),beside=T,col=1:5,density=20,angle=seq(45,225,45),border=T,xlab="# resurrections",ylab="# coins",main="Resurrections per coin",ylim=c(0,160))
legend("topright",c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol."),fill=1:4,density=20,angle=seq(45,225,45),border=T)
dev.off()

#analyze peak frequency, magnitude (% and $ rise, % and $ fall), split by size
ppp$peak<-as.Date(ppp$peak)
ppp<-merge(cssize,ppp,by.x="coin",by.y="market")

ppp$peak_value<-as.numeric(ppp$peak_value)
ppp$pre<-as.numeric(ppp$pre)
ppp$post<-as.numeric(ppp$post)

ppp$peakjump<-ppp$peak_value-ppp$pre
ppp$peakjumpfr<-100*ppp$peakjump/ppp$pre

ppp$peakfall<-ppp$peak_value-ppp$post
ppp$peakfallfr<-100*ppp$peakfall/ppp$peak_value

ppp<-merge(ppp,cvfd,by.x="coin",by.y="market",all.x=T)

#remove from consideration those with peaks in first week

ppp$ttpp<-as.Date(ppp$peak)-as.Date(ppp$fdate)

lastday<-aggregate(date~market,cv,max)
names(lastday)<-c("coin","lastday")
ppp<-merge(ppp,lastday,by="coin")
ppp$ttlp<-ppp$lastday-as.Date(ppp$peak)

ppp2<-ppp[ppp$ttpp>=7,]
ppp3<-ppp2[ppp2$ttlp>=7,]

pjf<-aggregate(peakjumpfr~size,ppp2,quantile,seq(0,1,0.1),na.rm=T)
pff<-aggregate(peakfallfr~size,ppp3,quantile,seq(0,1,0.1),na.rm=T)

vpp$peak<-as.Date(vpp$peak)
vpp<-merge(cssize,vpp,by.x="coin",by.y="market")

vpp$peak_value<-as.numeric(vpp$peak_value)
vpp$pre<-as.numeric(vpp$pre)
vpp$post<-as.numeric(vpp$post)

vpp$peakjump<-vpp$peak_value-vpp$pre
vpp$peakjumpfr<-100*vpp$peakjump/vpp$pre

vpp$peakfall<-vpp$peak_value-vpp$post
vpp$peakfallfr<-100*vpp$peakfall/vpp$peak_value

vpp<-merge(vpp,cvfd,by.x="coin",by.y="market",all.x=T)
vpp$ttpp<-as.Date(vpp$peak)-as.Date(vpp$fdate)

vpp<-merge(vpp,lastday,by="coin")
vpp$ttlp<-vpp$lastday-as.Date(vpp$peak)

vpp2<-vpp[vpp$ttpp>=7,]
vpp3<-vpp2[vpp2$ttlp>=7,]

vjf<-aggregate(peakjumpfr~size,vpp2,quantile,seq(0,1,0.1),na.rm=T)
vff<-aggregate(peakfallfr~size,vpp3,quantile,seq(0,1,0.1),na.rm=T)

vjf$peakjumpfr[vjf$peakjumpfr=="Inf"]<-6424277
vjf$peakjumpfr[vjf$peakumpfr<1]<-1

pdf("pvpeak.pdf",height=8,width=10)
par(mfrow = c(2,2),mar = c(5,5,4,1),las=1)
barplot(pjf$peakjumpfr[,2:10],beside=T,horiz=T,col=1:5,density=20,angle=seq(45,225,45),border=T,ylab="percentile",xlab="% price increase",main="% price increase from min 30 days prior to peak",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
legend("bottomright",rev(c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol.")),fill=5:1,density=20,angle=rev(seq(45,225,45)),border=T,cex=1.15)
barplot(pff$peakfallfr[,2:10],beside=T,horiz=T,col=1:5,density=20,angle=seq(45,225,45),border=T,ylab="percentile",xlab="% price decrease",main="% price decrease from peak to min 30 days after",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)

barplot(vjf$peakjumpfr[,2:10],beside=T,horiz=T,col=1:5,ylab="percentile",xlab="% volume increase",main="% volume increase from min 30 days prior to peak",log='x',cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
legend("bottomright",rev(c("<$1M total vol.","$1-10M total vol.","$10-100M total vol.","$100M-$1B total vol.",">$1B total vol.")),fill=5:1,cex=1.15)
barplot(vff$peakfallfr[,2:10],beside=T,horiz=T,col=1:5,ylab="percentile",xlab="% volume decrease",main="% volume decrease from peak to min 30 days after",cex.names=1.25,cex.axis=1.2,cex.lab=1.2)
dev.off()

ppkct<-data.frame(table(ppp$coin))
vpkct<-data.frame(table(vpp$coin))
names(ppkct)<-c("coin","numpeaks")
ppkct$numpkf<-factor(ppkct$numpeaks)
ppkct2<-merge(ppkct,cssize,by="coin")

names(vpkct)<-c("coin","numpeaks")
vpkct$numpkf<-factor(vpkct$numpeaks)
vpkct2<-merge(vpkct,cssize,by="coin")

acsu<-unique(acs[acs$abandoned,c("coin","size")])
acsru<-unique(acs[acs$resurrected,c("coin","size")])

pacsu<-unique(acs[acs$abandoned&!acs$resurrected,c("coin","size")])

cs2v2<-merge(cs2,ppp2,by.x=c("coin","p_peakd","size"),by.y=c("coin","peak","size"))
cs2v3<-merge(cs2,vpp2,by.x=c("coin","v_peakd","size"),by.y=c("coin","peak","size"))

sumtbl<-
data.frame(rbind(
c(sum(table(cvs$size)),table(cvs$size)),
c(sum(ppkct2$numpeaks),aggregate(numpeaks~size,ppkct2,sum)$numpeaks),
c(median(ppkct2$numpeaks),aggregate(numpeaks~size,ppkct2,median)$numpeaks),
c(sum(vpkct2$numpeaks),aggregate(numpeaks~size,vpkct2,sum)$numpeaks),
c(median(vpkct2$numpeaks),aggregate(numpeaks~size,vpkct2,median)$numpeaks),
round(c(100*median(cs2v2$p_pcti,na.rm=T),100*aggregate(p_pcti~size,cs2v2,median,na.rm=T)$p_pcti),0),
round(c(100*median(cs2v3$v_pcti,na.rm=T),100*aggregate(v_pcti~size,cs2v3,median,na.rm=T)$v_pcti),0),
c(length(acsu$coin),table(acsu$size)),
round(100*c(length(acsu$coin)/sum(table(cvs$size)),table(acsu$size)/table(cvs$size),0)),
c(length(acs$abandoned[acs$abandoned]),table(acs$abandoned,acs$size)[2,]),
round(c(median(cs3$otfa[cs3$abandoned],na.rm=T),aggregate(otfa~size,cs3[cs3$abandoned],median,na.rm=T)$otfa),0),
c(length(acsru$coin),table(acsru$size)),
round(100*c(length(acsru$coin)/length(acsu$coin),table(acsru$size)/table(acs$size)),0),
c(summary(acs$resurrected)[3],table(acs$resurrected,acs$size)[2,]),
round(c(median(acsize$months2res[acsize$resurrected],na.rm=T),aggregate(months2res~size,acsize[acsize$resurrected],median,na.rm=T)$months2res,NaN)),
c(length(pacsu$coin),table(pacsu$size)),
round(100*c(length(pacsu$coin)/sum(table(cvs$size)),table(pacsu$size)/table(cvs$size),0))
))

row.names(sumtbl)<-c("\\# coins","\\# price peaks (total)", "\\# price peaks (median)", "\\# volume peaks (total)", "\\# volume peaks (median)","\\% price increase\\n 1st peak (median)","\\% volume increase\\n 1st peak (median)","\\# coins abandoned","\\% coins abandoned","\\# abandonments","days abandoned (median)","\\# coins resurrected","\\% coins resurrected","\\# resurrections","months to resurrected (median)","\\# coins permanently\\\\abandoned","\\% coins permanently\\\\abandoned")

write.table(sumtbl,"sumtbltex.txt",sep="&",row.names=T,quote=F)
aggregate(numpeaks~size,ppkct2,median)
aggregate(numpeaks~size,vpkct2,median)
median(ppkct2$numpeaks)
median(vpkct2$numpeaks)


